﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DungeonCraft.DataModel.Interfaces;

namespace DungeonCraft.DataModel
{
    public class Level : ILevel
    {
        public enum GroundTypes
        {
            walkable,
            jumpable,
            impassable
        }

        private GroundTypes[,] groundTypes;

        public GroundTypes[,] GroundTypesList
        {
            get
            {
                return groundTypes;
            }
        }

        public Level(GroundTypes[,] groundTypes)
        {
            this.groundTypes = groundTypes;
        }

        public bool LocationIsAllowed(ILocation location, Player player)
        {
            return LocationIsAllowed(location.X, location.Y, player);
        }


        public bool LocationIsAllowed(int x, int y, Player player)
        {
            int left = Block.locationToBlock(x, Block.width);
            int right = Block.locationToBlock(x + player.Width, Block.width);
            int top = Block.locationToBlock(y, Block.height);
            int bottom = Block.locationToBlock(y + player.Height, Block.height);

            if (right >= groundTypes.GetUpperBound(0) || bottom >= groundTypes.GetUpperBound(1))
                return false;
            if (left < groundTypes.GetLowerBound(0) || top < groundTypes.GetLowerBound(1))
                return false;
            if (( groundTypes[left, top] == GroundTypes.jumpable || groundTypes[left, top] == GroundTypes.walkable ) && (groundTypes[right, bottom] == GroundTypes.jumpable || groundTypes[right, bottom] == GroundTypes.walkable) && player.Jumping)
                return true;
            if (groundTypes[left, top] == GroundTypes.walkable && groundTypes[right, bottom] == GroundTypes.walkable)
                return true;
            return false;
        }


    }
}
